set(CMAKE_SYSTEM_NAME Linux)

add_compile_options(-fPIC)

if(NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
    set(CMAKE_SYSTEM_PROCESSOR arm)
endif()

set(ARCH32_C_COMPILER_LIST arm-linux-gnueabihf-gcc arm-linux-gnu-gcc)
set(ARCH32_CXX_COMPILER_LIST arm-linux-gnueabihf-g++ arm-linux-gnu-g++)
set(ARCH64_C_COMPILER_LIST aarch64-linux-gnueabihf-gcc aarch64-linux-gnu-gcc)
set(ARCH64_CXX_COMPILER_LIST aarch64-linux-gnueabihf-g++ aarch64-linux-gnu-g++)
set(ARCH32_LIST arm armel armhf)
set(ARCH64_LIST arm64 aarch64)

set(IS_ARCH32 0)
set(IS_ARCH64 0)
list(FIND ARCH32_LIST ${CMAKE_SYSTEM_PROCESSOR} IS_ARCH32)
list(FIND ARCH64_LIST ${CMAKE_SYSTEM_PROCESSOR} IS_ARCH64)

if(${IS_ARCH32} EQUAL -1)
    set(IS_ARCH32 0)
else()
    set(IS_ARCH32 1)
endif()

if(${IS_ARCH64} EQUAL -1)
    set(IS_ARCH64 0)
else()
    set(IS_ARCH64 1)
endif()

if(NOT DEFINED CMAKE_C_COMPILER)
    if(IS_ARCH32)
        find_program(CMAKE_C_COMPILER NAMES ${ARCH32_C_COMPILER_LIST})
    elseif(IS_ARCH64)
        find_program(CMAKE_C_COMPILER NAMES ${ARCH64_C_COMPILER_LIST})
    else()
        message(STATUS "Unsupport CMAKE_SYSTEM_PROCESSOR")
    endif()
endif()

if(NOT DEFINED CMAKE_CXX_COMPILER)
    if(IS_ARCH32)
        find_program(CMAKE_CXX_COMPILER NAMES ${ARCH32_CXX_COMPILER_LIST})
    elseif(IS_ARCH64)
        find_program(CMAKE_CXX_COMPILER NAMES ${ARCH64_CXX_COMPILER_LIST})
    else()
        message(STATUS "Unsupport CMAKE_SYSTEM_PROCESSOR")
    endif()
endif()
